import request from '@/contentUtils/content.request'
import { labelTypePrivate } from '@/constant'
import store from '@/store'
import { getOriginFolderId } from '@/utils/index'

export function getFolderTree(labelType) {
    return request({
        url: `/folder/getAllFolderTree?type=${labelType}`,
        method: 'get'
    })
}

export function getFolder(folderId, RANGE_CODE) {
    //后台要求分享工作区模式下传入shotCode，目的是防止越权
    const shotCode = store.getters['share/isShareWorkspace'] ? store.state.share.shotCode : undefined
    return request({
        url: '/folder!getFolder.do',
        method: 'post',
        data: {
            ASSET_LABEL_SEQ: folderId,
            RANGE_CODE,
            shotCode
        }
    })
}

/** 计算文件夹中的文件信息 */
export function calculateFolderFilesApi(folderIds, folderType, isShare) {
    //后台要求分享工作区模式下传入shotCode，目的是防止越权
    const shotCode = store.getters['share/isShareWorkspace'] ? store.state.share.shotCode : undefined
    return request({
        url: '/folder/calculateFiles',
        method: 'post',
        data: {
            folderIds,
            folderType,//  SF/F
            isShare: !!isShare,  //是否是分享，true 或者 false ，对标之前的rangeCode=S
            shotCode
        }
    })
}

export function getSubFolder(folderId, LABEL_TYPE, LABEL_NAME, IS_PRECISE_QUERY, RANGE_CODE) {
    //后台要求分享工作区模式下传入shotCode，目的是防止越权
    const shotCode = store.getters['share/isShareWorkspace'] ? store.state.share.shotCode : undefined
    return request({
        url: '/folder!getSubFolder.do',
        method: 'post',
        data: {
            PNT_LABEL_ID: folderId || "-1",
            LABEL_NAME,
            LABEL_TYPE, IS_PRECISE_QUERY,
            RANGE_CODE,
            shotCode
        }
    })
}

/**
 * 搜索文件夹
 * @param {*} folderId
 * @param {*} LABEL_TYPE
 * @param {*} LABEL_NAME
 * @param {*} IS_PRECISE_QUERY
 * @param {*} RANGE_CODE
 * @returns
 */
export function searchFolders(folderId, LABEL_TYPE, LABEL_NAME) {
    return request({
        url: "/folder/listFolderBySearch",
        method: "post",
        data: {
            folderId: folderId || undefined,
            folderName: LABEL_NAME,
            folderType: LABEL_TYPE
        }
    });
}

export function editFolder(params) {
    return request({
        url: '/folder!editFolder.do',
        data: {
            ASSET_LABEL_SEQ: params.folderId,
            DATA: {
                TYPE: params.type,
                VALUE: params.value
            }
        }
    })
}

export function addFolder({ parentFolderId, name, labelType = labelTypePrivate }) {
    return request({
        url: '/folder!addFolder.do',
        data: {
            PNT_LABEL_ID: parentFolderId,
            LABEL_NAME: name,
            LABEL_TYPE: labelType
        }
    })
}

export function addFolderAndShareApi({ parentFolderId, name, shareInfo, type, labelType = labelTypePrivate }) {
    return request({
        url: '/folder!addFolderAndShare.do',
        data: {
            PNT_LABEL_ID: parentFolderId,
            LABEL_NAME: name,
            LABEL_TYPE: labelType,
            SHARE_INFO: shareInfo,
            FILE_TYPE: type,
        }
    })
}

export function editFolderPnt(params) {
    return request({
        url: '/folder!editFolderPnt.do',
        data: {
            ASSET_LABEL_SEQ: params.folderId,
            PNT_LABEL_ID: params.parentFolderId
        }
    })
}

export function getFolderDefTags(folderId) {
    return request({
        url: '/folder!getFolderDefTags.do',
        data: {
            ASSET_LABEL_SEQ: folderId
        }
    })
}

export function editFolderDefTags(params) {
    return request({
        url: '/folder!editFolderDefTags.do',
        data: {
            ASSET_LABEL_SEQ: params.labelId,
            tags: params.tags
        }
    })
}

export function getRangeInfo(rangeCode, taskId) {
    return request({
        url: '/folder!getRangeInfo.do',
        data: {
            RANGE_CODE: rangeCode,
            TASK_ID: taskId
        }
    })
}

export function delAssetFromFolder(folderId, assetIdArr) {
    return request({
        url: '/folder!delAssetFromFolder.do',
        data: {
            ASSETS: assetIdArr,
            ASSET_LABEL_SEQ: folderId
        }
    })
}

export function getTeamFolderTree() {
    return request({
        url: '/share!getSharedWithMeFolder.do'
    })
}

export function getTeamFolderAllTag(folderId, ind) {
    return request({
        url: '/folder!getFolderAllTag.do',
        data: {
            ASSET_LABEL_SEQ: folderId,
            GET_RECTAG_ONASSET_IND: ind
        }
    })
}

export function getAssetFolder(assetIds) {
    return request({
        url: '/folder!getAssetFolder.do',
        data: {
            ASSETS: assetIds
        }
    })
}

export function getAssetTeamFolder(assetIds) {
    return request({
        url: '/folder!getAssetTeamFolder.do',
        data: {
            ASSETS: assetIds
        }
    })
}

export function moveFolderCheck(params) {
    return request({
        url: '/asset-server/folder/move-check',
        data: {
            ASSET_LABEL_SEQ: params.nodeId,
            PNT_LABEL_ID: params.parentFolderId,
            PLACE: params.relativeNodeOrder,
            PLACE_TYPE: params.pos,
            TYPE: 'M',
            source: params.source
        }
    })
}

export function moveFolderTreeNode(params) {
    return request({
        url: '/asset-server/folder/move',
        data: {
            folder_id: params.nodeId,
            target_id: params.parentFolderId,
            index: params.relativeNodeOrder,
            type: params.pos,
            cover: params.cover,
            source: params.source
        }
    })
}

export function setAsCoverApi({COVER_ASSET_ID,...param}) {
    return request({
        url: '/folder!addFolderCover.do',
        data: {
            ASSET_LABEL_SEQ: param.folderId,
            IMAGE_URL: param.coverUrl  ,
            COVER_ASSET_ID
        }
    })
}

export function batchAddFolder({ parentFolderId, labelType, folders }) {
    return request({
        url: '/folder!batchAddFolder.do',
        data: {
            PNT_LABEL_ID: parentFolderId,
            LABEL_TYPE: labelType,
            FOLDERS: folders
        }
    })
}

export function batchAddFolderAndShare({ parentFolderId, labelType, folders, USR_ID }) {
    return request({
        url: '/folder!batchAddFolderAndShare.do',
        data: {
            PNT_LABEL_ID: parentFolderId,
            LABEL_TYPE: labelType,
            FOLDERS: folders,
            USR_ID,
        }
    })
}

export function copyToSharedFolder(param) {
    return request({
        url: '/asset!copyFolderToShare.do',
        data: {
            Source_Label_Seq: param.srcFolder,
            Target_Label_Seq: param.destFolder,
            Folder_Name: param.destName
        }
    })
}

export function getFolderByPath({ path, labelType }) {
    return request({
        url: '/folder!getFolderByPath.do',
        data: {
            ASSET_LABEL_PATH: path,
            LABEL_TYPE: labelType,
        }
    })
}

export function getSubFolderRecursively(labelType, folderId) {
    return request({
        url: '/folder!getSubFolderRecursively.do',
        data: {
            LABEL_TYPE: labelType,
            PNT_LABEL_ID: folderId
        }
    })
}

export function convertModelProject(params) {
    return request({
        url: "/folder!tdConverter.do",
        data: {
            labelSeq: getOriginFolderId(params.folderId),
            convertType: params.type,
            labelName: params.folderName
        }
    })
}

export function getSubFolderTree(params) {
    return request({
        method: "get",
        url: `/folder/getSubFolderTree?folderId=${getOriginFolderId(params.folderId)}&type=${params.folderType}`
    })
}

/**
 * 根据传入的id获取一层子文件夹
 * @param {*} folderIds 
 * @param {*} folderType 
 * @returns 
 */
export function getOneLevelFolderTreeApi(folderIds, folderType) {
    return request({
        method: "get",
        url: `/folder/getOneLevelFolderTree?folderIds=${folderIds.join(",")}&type=${folderType}`,
        // data: {
        //     folderIds: folderIds,
        //     type: folderType
        // }
    })
}


/**
 * 取消封面
 * @param {*} params 
 * @returns 
 */
export function resetFolderCover(params) {
    return request({
        method: "post",
        url: `/folder/removeCover`,
        data: params
    })
}

/**
 * 设置是否可移动
 * @param {Object} params
 * @param {*} params.folderId 文件夹id
 * @param {*} params.movable 1 可移动 0 不可移动
 * @returns 
 */
export function setMovableApi({ folderId, movable }) {
    return request({
        method: "post",
        url: `/folder/setMovable`,
        data: { folderId, movable }
    })
}

export function queryById(folderId) {
    return request({
        method: "get",
        url: `/folder/queryById/${folderId}`
    })
}

// 查询当前文件夹所有父id
export function getParentFoldersApi(folderId){
    return request({
        url:'/folder/getParentFolders',
        method:'get',
        params:{
            folderId
        }
    })
}